package com.nosnow.gmall.manage.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.nosnow.gmall.bean.*;
import com.nosnow.gmall.service.ListService;
import com.nosnow.gmall.service.ManageService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * @类名称：ManageController
 * @类描述：
 * @author: bingHeng
 * @Date
 * @Version: 1.0
*/
@CrossOrigin(origins = "http://domain2.com", maxAge = 3600)
@Controller
public class ManageController {

    @Reference
    private ManageService manageService;

    @Reference
    private ListService listService;

    /**
     * 说明：初始化index页面
     * @return
     */
    @RequestMapping("index")
    public String index() {
        return "index";
    }

    /**
     * 说明： 加载平台属性页面
     * @return
     */
    @RequestMapping("attrListPage")
    public String attrListPage() {
        return "attrListPage";
    }

    /**
     * 说明：加载一级分类
     * @return
     */
    @RequestMapping("getCatalog1")
    @ResponseBody
    public List<BaseCatalog1> getCatalog1(){
        List<BaseCatalog1> catalog1List = manageService.getCatalog1();
        return catalog1List;
    }

    /**
     * 说明：加载二级分类
     * @param map
     * @return
     */
    @RequestMapping("getCatalog2")
    @ResponseBody
    public List<BaseCatalog2> getCatalog2(@RequestParam Map<String,String> map){
        Long catalog1Id =  Long.valueOf(map.get("catalog1Id"));
        List<BaseCatalog2> catalog2List = manageService.getCatalog2(catalog1Id);
        return catalog2List;
    }

    /**
     * 说明：加载三级分类
     * @param map
     * @return
     */
    @RequestMapping("getCatalog3")
    @ResponseBody
    public List<BaseCatalog3> getCatalog3(@RequestParam Map<String,String> map){
        Long catalog2Id = Long.valueOf(map.get("catalog2Id"));
        List<BaseCatalog3> catalog3List = manageService.getCatalog3(catalog2Id);
        return catalog3List;
    }

    /**
     * 说明： 获取属性列表
     * @param map
     * @return
     */
    @RequestMapping("getAttrList")
    @ResponseBody
    public List<BaseAttrInfo>  getAttrList(@RequestParam Map<String,String> map){
        Long catalog3Id =   Long.valueOf(map.get("catalog3Id"));
        List<BaseAttrInfo> attrList = manageService.getAttrList(catalog3Id);
        return attrList;
    }

    /**
     * 说明：保存属性与属性值
     * @param baseAttrInfo
     * @return
     */
    @RequestMapping(value = "saveAttrInfo", method = RequestMethod.POST)
    @ResponseBody
    public String saveAttrInfo(BaseAttrInfo baseAttrInfo) {
        manageService.saveAttrInfo(baseAttrInfo);
        return "success";
    }

    /**
     * 点击编辑是调用
     * @paramparamMap
     * @return
     */
   /* @RequestMapping(value = "getAttrValueList", method = RequestMethod.POST)
    @ResponseBody
    public List<BaseAttrValue> getAttrValueList(@RequestParam Map<String, Object> paramMap) {
        String attrId = (String)paramMap.get("attrId");
        BaseAttrInfo attrInfo = manageService.getAttrInfo(attrId);
        return attrInfo.getAttrValueList();

    }*/

    @RequestMapping(value = "baseSaleAttrList", method = RequestMethod.POST)
    @ResponseBody
    public List<BaseSaleAttr> getBaseSaleAttrList() {
       return manageService.getBaseSaleAttrList();
    }

    @RequestMapping(value = "onSale", method = RequestMethod.GET)
    @ResponseBody
    public void onSale(String skuId) {
        SkuInfo skuInfo = manageService.getSkuInfo(skuId);
        SkuLsInfo skuLsInfo = new SkuLsInfo();
        try {
            BeanUtils.copyProperties(skuLsInfo, skuInfo);
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        listService.saveSkuLsInfo(skuLsInfo);
    }
}
